#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std; 
class Solution {
public:
    int lengthOfLastWord(string s) {
        int n = s.size();
        while (n > 0 && s[n - 1] == ' ')n--;
        int i = n - 1;
        while (i >= 0)
        {
            if (s[i] != ' ')i--;
            else break;
        }
        return n - 1 - i;
    }
};